import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Login {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入用户名，由长度大于等于6的英文字母构成：");
        String userName = scanner.next();
        if (!isValidUserName(userName)) {
            System.out.println("用户名不符合规范！");
            return;
        }

        System.out.println("请输入您的生日，格式为：yyyyMMdd");
        String birthDateStr = scanner.next();
        if (!isValidBirthDate(birthDateStr)) {
            System.out.println("生日格式错误或不在有效年龄范围内！");
            return;
        }

        String password = generatePassword(userName, birthDateStr);

        System.out.println("请输入密码：");
        String inputPassword = scanner.next();
        if (!isValidPassword(password, inputPassword)) {
            System.out.println("密码不正确！");
        } else {
            System.out.println("欢迎您，" + userName + "！");
        }
    }

    //判断用户名是否符合规范
    private static boolean isValidUserName(String userName) {
        if (userName.length() < 6) {
            return false;
        }

        //判断是否全由字母构成
        return userName.chars().allMatch(Character::isLetter);
    }

    //判断生日是否正确以及是否在有效年龄范围内
    private static boolean isValidBirthDate(String birthDateStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");

        //解析日期，如果解析不成功则说明日期格式不正确，返回false
        LocalDate birthDate;
        try {
            birthDate = LocalDate.parse(birthDateStr, formatter);
        } catch (Exception e) {
            return false;
        }

        //判断生日是否在有效年龄范围内
        LocalDate today = LocalDate.now();
        if (birthDate.isAfter(today.minusYears(6)) || birthDate.isBefore(today.minusYears(100))) {
            return false;
        } else if (birthDate.getMonthValue() == 2 && birthDate.getDayOfMonth() > 29) {
            return false;
        } else if ((birthDate.getMonthValue() == 4 || birthDate.getMonthValue() == 6 ||
                birthDate.getMonthValue() == 9 || birthDate.getMonthValue() == 11) && birthDate.getDayOfMonth() > 30) {
            return false;
        } else if (birthDate.getDayOfMonth() > 31) {
            return false;
        }

        return true;
    }

    //生成密码
    private static String generatePassword(String userName, String birthDateStr) {
        return userName.concat("@").concat(birthDateStr);
    }

    //判断密码是否正确
    private static boolean isValidPassword(String correctPassword, String inputPassword) {
        return correctPassword.equals(inputPassword);
    }
}
